﻿using System;
using System.Windows.Forms;

namespace CutterTool
{
    public partial class frmMain : Form
    {
        private Worker w;
        public frmMain()
        {
            InitializeComponent();
            w = new Worker();
            w.createCutterList();
        }

        #region Event Methods

        /// <summary>
        /// Main form load event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmMain_Load(object sender, EventArgs e)
        {
            if (this.checkDatasources() == false)
            {
                string mess = "";
                mess += "This appears to be your first time using this program.\n";
                mess += "Before use, set the datasource by going to \"File->Set Datasource\".\n";
                mess += "Clicking \"OK\" will open the data sources window for you.";
                if (MessageBox.Show(mess, "First Run", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    frmSetDataSource fsd = new frmSetDataSource();
                    fsd.Show();
                    fsd.Activate();
                    fsd.BringToFront();
                }
            }
        }

        /// <summary>
        /// Menu strip "About" click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmAbout fa = new frmAbout();
            fa.Show();
            fa.BringToFront();
        }

        /// <summary>
        /// Menu strip "Set Datasource" click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void setDatasourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSetDataSource fsd = new frmSetDataSource();
            fsd.Show();
            fsd.BringToFront();
        }

        /// <summary>
        /// Menu strip "Exit" click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Menu strip "View Datasource" click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void viewCurrentDatasourcesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string mess = "";
            mess += "Datasource:  " + Properties.Settings.Default.datasourcePath;
            mess += " [" + Properties.Settings.Default.datasourceTable + "]\n";

            MessageBox.Show(mess, "Cutter Lookup Datasources", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// Button "Locate!" click event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void butGo_Click(object sender, EventArgs e)
        {
            if (tbInput.Text.Equals(""))
            {
                if (System.DateTime.Now.Second % 2 == 0)
                {
                    MessageBox.Show("The free version of this software does not incorporate mind reading.\nPlease enter a name in the box.", "No Name", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("Are you looking for \"The Man With No Name\"?\nIf not, please enter a name in the box.", "No Name", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                return;
            }

            if (this.checkDatasources() == false)
            {
                MessageBox.Show("Datasources not set. Go to \"File->Set Datasource\" before proceeding.", "Datasources not found", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // Run the lookup
            this.cutterLookup();
        }

        #endregion

        #region Custom Methods

        /// <summary>
        /// Driver method for the lookup process.
        /// </summary>
        private void cutterLookup()
        {
            string cuttercode;
            string prefix;
            string postfix;
            try
            {
                w.findCutterCode(tbInput.Text, out cuttercode, out prefix, out postfix);
                this.tbCutterCode.Text = cuttercode;
                this.labPrefix.Text = prefix;
                this.labPostfix.Text = postfix;
                System.Windows.Forms.Clipboard.SetText(cuttercode);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error finding Cutter code. Try again or check you datasource.\n" + ex.Message, "Lookup Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Checks that the datasource settings have been initialized. Returns true if so or false if not.
        /// </summary>
        /// <returns></returns>
        private bool checkDatasources()
        {
            if (Properties.Settings.Default.datasourcePath.Equals("None"))
            {
                return false;
            }

            return true;
        }

        #endregion


    }
}
